package com.after.saas.file.handler;

import com.after.saas.file.constants.MedalLevelEnum;
import com.after.saas.file.constants.MedalTitleEnum;
import lombok.Getter;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Description
 * @Classname MedalHandler
 * @Author hjchen
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2023/4/7 14:12
 */

@Getter
public abstract class MedalHandler {

    Map<String, String> levelMap = new TreeMap<>(Comparator.reverseOrder());

    /**
     * 勋章
     *
     * @param
     * @return java.lang.String
     * @author hjchen
     * @date 2023/4/7 15:21
     **/
    public abstract MedalTitleEnum getMedalEnum();

    /**
     * 计算积分
     *
     * @param sumPoints
     * @return void
     * @author hjchen
     * @date 2023/4/7 14:14
     **/
    public abstract MedalRecord saveMedalRecord(BigDecimal sumPoints);

    public MedalRecord buildMedalRecord(BigDecimal sumPoints) {
        MedalRecord medalRecord = new MedalRecord();
        medalRecord.setIsWindow("0");
        medalRecord.setCreateTime(LocalDateTime.now());
        medalRecord.setTitle(getMedalEnum().getTitle());
        medalRecord.setMedalLevel(getLevel(sumPoints).getLevel());
        medalRecord.setUserId("after");
        return medalRecord;
    }

    /**
     * 获取勋章等级
     *
     * @param sumPoints
     * @return void
     * @author hjchen
     * @date 2023/4/7 16:03
     **/
    public MedalLevelEnum getLevel(BigDecimal sumPoints) {
        intiLevelMap();
        for (MedalLevelEnum levelEnum : MedalLevelEnum.values()) {
            if (sumPoints.compareTo(new BigDecimal(levelMap.get(levelEnum.name()))) < 1) {
                return levelEnum;
            }
        }
        return MedalLevelEnum.LV1;
    }

    /**
     * 初始化勋章等级
     *
     * @return void
     * @author hjchen
     * @date 2023/4/7 16:03
     **/
    public abstract void intiLevelMap();
}
